<?php

include_once("../data/dao/CollectionDAO.php");
include_once("../application/collection/CollectionUtils.php");
include_once("../data/dao/GameDAO.php");
include_once("../data/dao/FriendCodeDAO.php");
include_once("../data/dao/WishListDAO.php");
include_once("../conf.php");
include_once("../services/FacebookProxy.php");

/**
 * This class acts as the controller for adding games
 * to a users collection.
 * 
 * @author Matt Makins - 20071230
 */
class GameHandler{
	
	private $user;
	
	public function GameHandler(){
		
		$facebook = new FacebookProxy();
		// having to use requireLogin, because getLoggedInUser 
		// wasn't working in IE!
		$this->user = $facebook->requireLogin();
		
		if($_REQUEST['do'] == "collection-add")
			$this->addGameToCollection($_REQUEST['id'], $this->user);
		if($_REQUEST['do'] == "collection-delete")
			$this->deleteGameFromCollection($_REQUEST['id'], $this->user);
		if($_REQUEST['do'] == "wishlist-add")
			$this->addGameToWishList($_REQUEST['id'], $this->user);
		if($_REQUEST['do'] == "wishlist-delete")
			$this->deleteGameFromWishList($_REQUEST['id'], $this->user);
	}
	
	private function addGameToCollection($id, $userID){
		
		if(CollectionUtils::gameAlreadyOwned($id, $userID))
			return;
			
		$collectionDAO = new CollectionDAO();

		try{
			$collection = $collectionDAO->getCollection($userID);
			
			if($collection == null)
				$games = array();
			else	
				$games = $collection->getGames();
			
			array_push($games, $id);
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem retrieving your game collection<br/>";
			echo "Exception: " . $e->getMessage();
		}
		
		try{
			$collectionDAO->updateCollection(implode(",",$games), $userID);
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem adding game to your collection<br/>";
			echo "Exception: " . $e->getMessage();
		}
	
		//if game was in wish list, remove it
		if (WishListUtils::gameAlreadyInWishList($id, $this->user)){
			$this->deleteGameFromWishList($id, $this->user);
		}
	
		$facebook = new FacebookProxy();
		$facebook->updateProfile();
		
		header("Location: index.php");
	}

	private function deleteGameFromCollection($id, $userID){
	
		if(!CollectionUtils::gameAlreadyOwned($id, $userID))
			return;

		$currentCollection = $this->getCollectionArray($userID);		
		
		$newCollection = array();
		foreach($currentCollection as $g){
			if($g != $id)
				array_push($newCollection, $g);
		}
		
		try{
			$collectionDAO = new CollectionDAO();
			$collectionDAO->updateCollection(implode(",",$newCollection), $userID);
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem adding game to your collection<br/>";
			echo "Exception: " . $e->getMessage();
		}
		
		$fcdao = new FriendCodeDAO();
		$fcdao->deleteFriendCode($userID, $id); 
		
		$facebook = new FacebookProxy();
		$facebook->updateProfile();
		
		header("Location: index.php");
	}

	private function getCollectionArray($userID){
		
		try{
			$collectionDAO = new CollectionDAO();
			$collection = $collectionDAO->getCollection($userID);
			
			if($collection == null)
				$games = array();
			else	
				$games = $collection->getGames();
			
			return $games;
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem retrieving your game collection<br/>";
			echo "Exception: " . $e->getMessage();
		}
	}

	private function addGameToWishList($id, $userID){
		
		if(WishListUtils::gameAlreadyInWishList($id, $userID))
			return;
			
		$wldao = new WishListDAO();

		try{
			$wishList = $wldao->getWishList($userID);
			
			if($wishList == null)
				$games = array();
			else	
				$games = $wishList;
			
			array_push($games, $id);
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem retrieving your game wish list<br/>";
			echo "Exception: " . $e->getMessage();
		}
		
		try{
			$wldao->updateWishList(implode(",",$games), $userID);
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem adding game to your wish list<br/>";
			echo "Exception: " . $e->getMessage();
		}
	
		$facebook = new FacebookProxy();
		$facebook->updateProfile();
		
		header("Location: index.php");
	}

	private function deleteGameFromWishList($id, $userID){
	
		if(!WishListUtils::gameAlreadyInWishList($id, $userID))
			return;

		$currentWishList = $this->getWishListArray($userID);		
		
		$newWishList = array();
		foreach($currentWishList as $g){
			if($g != $id)
				array_push($newWishList, $g);
		}
		
		try{
			$wldao = new WishListDAO();
			$wldao->updateWishList(implode(",",$newWishList), $userID);
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem adding game to your wish list<br/>";
			echo "Exception: " . $e->getMessage();
		}
		
		$facebook = new FacebookProxy();
		$facebook->updateProfile();
		
		header("Location: index.php");
	}

	private function getWishListArray($userID){
		
		try{
			$wldao = new WishListDAO();
			$wishList = $wldao->getWishList($userID);
			
			if($wishList == null)
				$games = array();
			else	
				$games = $wishList;
			
			return $games;
		}
		catch (Exception $e){
			//TODO: add some way of monitoring exceptions, ie email alerts?			
			echo "Problem retrieving your game wish list<br/>";
			echo "Exception: " . $e->getMessage();
		}
	}
}
$cu = new GameHandler();
?>
